;;; init.el --- Emacs configuration -*- lexical-binding: t -*-

;; Author: Yuanchen Xie
;; Package-Requires: ("GNU/Emacs")
;; Homepage: gitee.com/cor5corpii/emacs.d
;; Keywords: Emacs, .emacs.d

;;; Commentary:

;; This project is my Emacs custom configurations,
;; Not part of GNU Emacs.

;;; Code:

;; 不修飾frame-title
(setq default-frame-alist '((undecorated . t)))
(add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
(add-to-list 'default-frame-alist '(ns-appearance . dark))

;; -------- Speed up Startup Process --------
;; Optimize Garbage Collection for Startup
(setq gc-cons-threshold most-positive-fixnum)

;; Optimize `auto-mode-alist`
(setq auto-mode-case-fold nil)

(unless (or (daemonp) noninteractive init-file-debug)
  ;; Temporarily suppress file-handler processing to speed up startup
  (let ((default-handlers file-name-handler-alist))
    (setq file-name-handler-alist nil)
    ;; Recover handlers after startup
    (add-hook 'emacs-startup-hook
              (lambda ()
                (setq file-name-handler-alist
                      (delete-dups
                       (append file-name-handler-alist default-handlers))))
              101)))

;; -------- Configure Load Path --------
;; Add "config" and "site-lisp" to the beginning of `load-path`
(defun update-load-path (&rest _)
  "Update the `load-path` to prioritize personal configurations."
  (dolist (dir '("site-lisp" "config"))
    (push (expand-file-name dir user-emacs-directory) load-path)))

;; Ensure these functions are called after `package-initialize`
(advice-add #'package-initialize :after #'update-load-path)

;; Initialize load paths explicitly
(update-load-path)

(require '+package)

(require '+base)
(require '+function)                    ; custom functions
;; (require '+submodule)

(require '+UI)                          ; (& window frame mode-line theme)
(require '+fonts)

(require '+edit)
(require '+auto-save)
(require '+cursor)
(require '+highlight-display)
(require '+completion)                  ; search & completion
(require '+color-rg)                    ; search based on ripgrep

(require '+shell)                       ; shell

(require '+markdown)
(require '+comment)                     ; separedit
(require '+hanh)                        ; 漢語(hanh-ngyoq)輸入與翻譯
(require '+tex)                         ; LaTeX

(require '+program)
(require '+lsp-bridge)
(require '+cc)                          ; c/c++
(require '+python)
(require '+ESS-R)                       ; Emacs Speaks Statistics, R
(require '+ESS-SAS)                     ; Emacs Speaks Statistics, SAS

(require '+vcs)                         ; version control system

(require '+msWin)                       ; for MS Windows

;; -------- no custom-file --------
(setq custom-file (expand-file-name "custom.el" user-emacs-directory))

;; load silently
(when (file-exists-p custom-file)
  (load custom-file 'no-error 'no-message))

;; processing before each quit
(defun cor5-bye-emacs ()
  "Remove custom.el & packages when quit."
  (interactive)
  ;; delete custom.el
  (when (file-exists-p custom-file)
    (eshell-command (concat "rm " custom-file)))
  ;; remove packages Not in the list
  (package-autoremove)
  ;; C-x C-c
  (save-buffers-kill-terminal))

(global-set-key (kbd "C-x C-c") #'cor5-bye-emacs)

;;; init.el ends here
